from flask import session
from flask_restful import Resource, reqparse

from app.models import User

parse = reqparse.RequestParser()
parse.add_argument('u_name', type=str, help='请输入用户名')
parse.add_argument('u_password', type=str, help='请输入密码')


class LoginResource(Resource):
    def get(self):
        pass

    def post(self):
        args = parse.parse_args()
        name = args.get('u_name')
        password = args.get('u_password')
        user = User.query.filter(User.u_name == name)
        if user.count() == 0:
            return {'msg': 'please register', 'status': 404}
        user = user.first()
        if user.u_password == password:
            session['u_name'] = name
            session['u_password'] = password
            return {'msg': 'login success', 'status': 200}
        return {'msg': 'password error', 'status': 404}

    def put(self):
        pass

    def delete(self):
        pass
